package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.patientsource.PatientSourceAddReqDTO;
import com.yixin.hospital.entity.dto.req.patientsource.PatientSourceEditReqDTO;
import com.yixin.hospital.service.PatientSourceService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/patient-source")
@Api(value = "患者来源管理", tags = "患者来源管理")
public class PatientSourceController {

    @Resource
    private PatientSourceService patientSourceService;

    @GetMapping("/list")
    @ApiOperation(value = "患者来源列表")
    public ApiResult list() {
        return ApiResult.success(patientSourceService.list());
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加患者来源")
    public ApiResult add(@Valid @RequestBody PatientSourceAddReqDTO patientSourceAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return patientSourceService.add(patientSourceAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑患者来源")
    public ApiResult edit(@Valid @RequestBody PatientSourceEditReqDTO patientSourceEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return patientSourceService.edit(patientSourceEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除患者来源")
    public ApiResult remove(@PathVariable int id) {
        patientSourceService.removeById(id);
        return ApiResult.success();
    }

}
